<?php
/**
 * Created by PhpStorm.
 * User: AA
 * Date: 2016/12/14
 * Time: 16:22
 *
 * *  Action
 * 消息操作类
 */

namespace Home\Action;

use Home\Logic;


class MessageAction
{
    public function __construct() {
        $this->Message = D('Message','Logic');
    }

    /**
     *  获取 当前用户 的信息
     * @param int $uid
     * @return array
     */

    /**
     * @param null $id
     * @param $start
     * @param $length
     * @param $type
     * @return array $data
     */

    public function getMessage( $id = null,$start,$length,$type )
    {
        if ( $type == "C端" )
        {
            $uid = (int)$_SESSION['id']?$_SESSION['id']:$id;
            // 先判断是否有最新的系统消息
            $data_message = $this->Message->getNotMessage( $uid );
            if ( !empty( $data_message ) )
            {
                foreach ( $data_message as $key => $value )
                {
                    $data_message[$key]['create_time'] = time();
                    $data_message[$key]['user_id'] = $uid;
                }
                // 入库系统推送消息
                $this->Message->setMessage( $data_message );
            }
            // 获取数据
            $data = $this->Message->getMessage( $uid,$start,$length );
        }
        else if( $type == "B端" )
        {
            if ( $id == null )
            {
                return false;die();
            }
            else
            {
                $data = $this->Message->getStoreMessage( $id,$start,$length );
            }
        }
        return $data;die();

    }

    /**
     * 查看某条信息的内容
     * @param int $id 站内信的ID
     * @param int $type 站内信类型 B端 =》2 C端 =》3
     * @param int $uid 用户ID
     * @return array
     */

    public function getContent( $id ,$type,$uid = null )
    {
        $id = (int)trim( $id );
        if ( $id < 1 )
        {
            return fasle;die();
        }
        if ( $type!=2 && $type!=3 )
        {
            return false;die();
        }
        $uid = (int)$_SESSION['id']?$_SESSION['id']:$uid;
        $data = $this->Message->getContent( $id,$type,$uid );
        if ( empty( $data ) )
        {
            return false;
            die();
        }
        else
        {
            return $data;
            die();
        }

    }

    /**
     *  添加 站内信 内容
     * @access public
     * @param string $content 消息内容
     * @param int $type 消息类型
     * @return int
     */

    public function setMessageText( $content, $type )
    {
        $data['message'] = trim( $content );
        $data['type'] = (int)$type;
        $data['create_time'] = time();
        $data = $this->Message->setMessageText( $data );
        return $data;
        die();
    }

    /**
     *  添加 站内信 详细信息
     * @access public
     * @param int $order_id 订单id
     * @param int $action_id 活动id
     * @param int $user_id 用户id （C 端）
     * @param int $store_id 店铺id（B 端）
     * @param int $message_id 站内信息id
     * @param int $action_id 活动id
     * @return int
     */

    public function setMessage( $order_id,$action_id,$user_id = null,$store_id,$message_id )
    {
        $data['order_id'] = (int)$order_id;
        $data['action_id'] = (int)$action_id;
        $data['user_id'] = (int)$_SESSION['id']?$_SESSION['id']:$user_id;
        $data['store_id'] = (int)$store_id;
        $data['order_id'] = (int)$order_id;
        $data['message_id'] = (int)$message_id;
        $data['create_time'] = time();
        if( $data['order_id']<1 ||
            $data['action_id']<1 ||
            $data['user_id']<1 ||
            $data['store_id']<0 ||
            $data['message_id'] <1 ){
            return false;
        }
        $res = $this->Message->setMessage( $data );
        if ( !$res )
        {
            return false;
        }
        else
        {
            return $res;
        }
    }


    /**
     *  修改 站内信 阅读状态
     * @access public
     * @param int $id 数据id
     * @return int
     */

    public function setReadStatus( $id, $uid = null  )
    {
        $data[ 'id' ] = (int)$id;
        if ( $data['id']<1 )
        {
            return fasle;
        }
        // $data[ 'user_id' ] = (int)$_SESSION['id']?$_SESSION['id']:$uid;
        $res =  $this->Message->setReadStatus( $data );
        if ( !$res )
        {
            return false;
        }
        else
        {
            return $res;
        }
    }

    /**
     *  修改 站内信 删除状态
     * @access public
     * @param int $id 数据id
     * @return int
     */

    public function setDelStatus( $id, $uid = null  )
    {
        $data[ 'id' ] = (int)$id;
        if ( $data['id']<1 )
        {
            return fasle;
        }
        // $data[ 'user_id' ] = (int)$_SESSION['id']?$_SESSION['id']:$uid;
        $res = $this->Message->setDelStatus( $data );
        if ( !$res )
        {
            return false;
        }
        else
        {
            return $res;
        }
    }

}